package com.wika.basics.base

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.wika.basics.base.fragment.OnNewIntent
import com.wika.basics.dialog.LoadingDialog
import com.wika.basics.ext.saveAs
import com.wika.basics.ext.saveAsUnChecked
import com.wika.basics.widget.TipsToast
import java.lang.reflect.ParameterizedType

/**
 *@Description: Fragment基类
 *@Date: 2024/7/26 17:53
 *@Author: WangWeiShuo
 */
abstract class BaseFragment<VB: ViewBinding>: Fragment(),OnNewIntent {
    private var TAG: String? = this::class.java.simpleName

    lateinit var mBinding:VB
    private var mIsViewCreate = false

    private val loadingDialog by lazy {
        LoadingDialog.Builder(requireContext())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return getContentView(inflater, container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mIsViewCreate = true
        initView(view, savedInstanceState)
        initData()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        //手动切换首页tab时，先调用此方法设置fragment的可见性
        if (mIsViewCreate) {
            onFragmentVisible(isVisibleToUser)
        }
    }

    override fun onResume() {
        super.onResume()
        if (userVisibleHint) {
            onFragmentVisible(true)
        }
    }

    override fun onStop() {
        super.onStop()
        if (userVisibleHint) {
            onFragmentVisible(false)
        }
    }

    open fun onFragmentVisible(isVisibleToUser: Boolean) {
        Log.w(TAG,"onFragmentVisible-${TAG}-isVisibleToUser:$isVisibleToUser")
    }

    /**
     * 设置布局View
     */
//    open fun getContentView(inflater: LayoutInflater, container: ViewGroup?): View {
//        return inflater.inflate(getLayoutResId(), null)
//    }

    fun getContentView(inflater: LayoutInflater, container: ViewGroup?): View {
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        return mBinding!!.root
    }

    /**
     * 初始化视图
     * @return Int 布局id
     * 仅用于不继承BaseDataBindFragment类的传递布局文件
     */
//    abstract fun getLayoutResId(): Int

    /**
     * 初始化布局
     * @param savedInstanceState Bundle?
     */
    abstract fun initView(view: View, savedInstanceState: Bundle?)

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 加载提示框
     */
    fun showLoading(msg: String?) {
        loadingDialog.showLoading(msg)
    }

    /**
     * 关闭提示框
     */
    fun dismissLoading() {
        loadingDialog.dismissLoading()
    }

    /**
     * Toast
     * @param msg Toast内容
     */
    fun showToast(msg: String) {
        TipsToast.showTips(msg)
    }

    /**
     * 成功提示
     * @param msg String
     */
    fun showSuccessToast(msg: String) {
        TipsToast.showSuccessTips(msg)
    }

    /**
     * 失败提示
     * @param msg String
     */
    fun showFailToast(msg: String) {
        TipsToast.showFailedTips(msg)
    }

    /**
     * 警告提示
     * @param msg String
     */
    fun showWarningToast(msg: String) {
        TipsToast.showWarningTips(msg)
    }


    override fun onDestroy() {
        super.onDestroy()
    }
}